package com.huadu.hdoj.controller;

import com.huadu.hdoj.common.AliOssUtil;
import com.huadu.hdoj.common.BaseResponse;
import com.huadu.hdoj.common.ResultUtils;
import java.io.IOException;
import java.util.UUID;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传接口
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public BaseResponse<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("文件上传，{}",file);
        String originalFilename = file.getOriginalFilename();//原始文件名
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String objectName = UUID.randomUUID().toString()+extension;
        String filePath = aliOssUtil.upload(file.getBytes(), objectName);
        return ResultUtils.success(filePath);

    }
}
